// laydate 单个日期选择
laydate.render({
  elem: '.item-datetime',
  type: 'datetime',
  trigger: 'click',
})

// laydate 日期范围选择
laydate.render({
  elem: '.item-datetime-range',
  range: '~',
  type: 'datetime',
  trigger: 'click',
})

// icheck 初始化
$('.i-checks').iCheck({
  checkboxClass: 'icheckbox_square-blue',
  radioClass: 'iradio_square-blue',
});

// ajaxModel 异步弹出框
$("body").delegate("*[data-trigger='ajaxModel']", "click", function () {
  var dataUrl = $(this).attr("data-url");
  var dataMax = eval($(this).attr("data-max"));
  var dataTitle = $(this).attr("data-title");
  $.confirm({
    title: dataTitle ? dataTitle : "标题",
    columnClass: dataMax ? 'col-md-9 col-md-offset-1' : 'col-md-6 col-md-offset-3',
    content: 'url:' + dataUrl,
    closeIcon: true,
    backgroundDismiss: true,
    buttons: {'关闭': {}}
  })
})

// 以下为修改jQuery Validation插件兼容Bootstrap的方法
$.validator.setDefaults({
  highlight: function (element) {
    $(element).closest('.form-group').removeClass('has-success').addClass(
        'has-error');
  },
  success: function (element) {
    element.closest('.form-group').removeClass('has-error').addClass(
        'has-success');
  },
  errorElement: "span",
  errorPlacement: function (error, element) {
    if (element.is(":radio") || element.is(":checkbox")) {
      error.appendTo(element.parent().parent().parent());
    } else {
      error.appendTo(element.parent());
    }
  },
  errorClass: "help-block m-b-none",
  validClass: "help-block m-b-none"
})

// 空格截取
function trim(value) {
  if (value == null) {
    return "";
  }
  return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
}

// 判断字符串是否为空
function isEmpty(value) {
  if (value == null || this.trim(value) == "" || value == undefined || value == "undefined") {
    return true;
  }
  return false;
}

// 判断移动端
function isMobile() {
  return navigator.userAgent.match(/(Android|iPhone|SymbianOS|Windows Phone|iPad|iPod)/i);
}

// 打开遮罩层
function loading(message) {
  $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
}

// 关闭遮罩层
function closeLoading() {
  setTimeout(function(){
    $.unblockUI();
  }, 50);
}

// 禁用按钮
function disable() {
  var doc = window.top == window.parent ? top.window.document : window.parent.document;
  $("a[class*=layui-layer-btn]", doc).addClass("layer-disabled");
}

// 启用按钮
function enable() {
  var doc = window.top == window.parent ? top.window.document : window.parent.document;
  $("a[class*=layui-layer-btn]", doc).removeClass("layer-disabled");
}

// 关闭窗体
function closePage(index) {
  if (isEmpty(index)){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
  } else {
    top.layer.close(index);
  }
}

function msg(content, type) {
  top.layer.msg(content);
}

function openPage(title, url, width, height, callback) {
  // 如果是移动端，就使用自适应大小弹窗
  if (isMobile()) {
    width = 'auto';
    height = 'auto';
  }
  if (isEmpty(title)) {
    title = false;
  }
  if (isEmpty(url)) {
    url = "/404.html";
  }
  if (isEmpty(width)) {
    width = 900;
  }
  if (isEmpty(height)) {
    height = ($(window).height() - 50);
  }
  if (isEmpty(callback)) {
    callback = function(index, layero) {
      var iframeWin = layero.find('iframe')[0];
      iframeWin.contentWindow.submitHandler(index, layero);
    }
  }
  top.layer.open({
    type: 2,
    area: [width + 'px', height + 'px'],
    fix: false,
    //不固定
    maxmin: true,
    shade: 0.3,
    title: title,
    content: url,
    btn: ['确定', '关闭'],
    // 弹层外区域关闭
    shadeClose: true,
    yes: callback,
    cancel: function(index) {
      return true;
    },
    success: function () {
      $(':focus').blur();
    }
  });
}

// 保存信息 刷新表格
function saveInfo(url, data, callback) {
  var config = {
    url: url,
    type: "post",
    dataType: "json",
    data: data,
    beforeSend: function () {
      loading("正在处理中，请稍候...");
      disable();
    },
    success: function(result) {
      if (typeof callback == "function") {
        callback(result);
      }
      successCallback(result);
    }
  };
  $.ajax(config)
}

// 成功回调执行事件（父窗体静默更新）
function successCallback(result) {
  closePage();
  var topWindow = $(window.parent.document);
  $('#J_iframe', topWindow)[0].contentWindow.refreshTable();
  closeLoading();
  enable();
}


